package com.sysu.simulation.widget;

import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.CoordinatorLayout;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.sysu.simulation.R;


public class BottomPasswordDialog {

    private Context mContext;
    private View mRootView;
    private EditText mEditText;
    private Button mSubmit;
    private BottomDialog mDialog;

    private BottomPasswordDialog(Context context) {
        mContext = context;
    }

    public static BottomPasswordDialog getDialog(Context context) {
        BottomPasswordDialog dialog = new BottomPasswordDialog(context);
        dialog.mRootView = LayoutInflater.from(context).inflate(R.layout.confirm_register_bottom, null, false);
        dialog.initView();
        return dialog;
    }

    public void initView() {
        mDialog = new BottomDialog(mContext);
        mDialog.setContentView(mRootView);
        mEditText = (EditText)mRootView.findViewById(R.id.confirm_password_input);
        mSubmit = (Button)mRootView.findViewById(R.id.confirm_password_submit);

        mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                hideKeyboard(mEditText);
            }
        });

        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mSubmit.setEnabled(s.length() > 0);
            }
        });
    }

    public void show() {
        mDialog.show();
        /*mRootView.postDelayed(new Runnable() {
            @Override
            public void run() {
                showKeyboard(mEditText);
            }
        },50);*/
    }

    public void dismiss() {
        hideKeyboard(mEditText);
        mDialog.dismiss();
    }

    public void setSubmitListener(View.OnClickListener l) {
        mSubmit.setOnClickListener(l);
    }

    public EditText getEditText() {
        return mEditText;
    }

    private void hideKeyboard(EditText currentFocus) {
        if(currentFocus.isFocused()) currentFocus.clearFocus();
        InputMethodManager m = (InputMethodManager) currentFocus.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        m.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
    }

    private void showKeyboard(View attemptFocus) {
        if(attemptFocus == null) return;
        attemptFocus.setFocusable(true);
        attemptFocus.setFocusableInTouchMode(true);
        if(!attemptFocus.isFocused()) attemptFocus.requestFocus();
        InputMethodManager m = (InputMethodManager) attemptFocus.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        m.showSoftInput(attemptFocus, 0);
    }
}
